﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;

namespace FeedTheWalrus
{
    public class FishManager
    {
        List<Fish> fishes;
        TimeSpan spawnTime;
        TimeSpan totalTime;
        Walrus walrus;

        SoundEffect fail;
        SoundEffect success;

        private const int maxFish = 10;

        public static ThunderFish.SpriteSheet fishSpriteSheet;

        public void OnNavigatedTo(ContentManager content)
        {
            walrus = new Walrus();
            fishes = new List<Fish>();
            totalTime = new TimeSpan(0, 0, 2);

            walrus.OnNavigatedTo(content);

            fishSpriteSheet = new ThunderFish.SpriteSheet(content.Load<Texture2D>("fish"), 2, 1);

            spawnTime = totalTime;

            fail = content.Load<SoundEffect>("Fail");
            success = content.Load<SoundEffect>("Success");
        }

        public void OnNavigatedFrom()
        {
            fishes.Clear();
        }

        public void Reset()
        {
            fishes.Clear();
            ScoreManager.health = 3;
            ScoreManager.playerScore = 0;
        }

        public void TakeInput(TouchCollection input)
        {
            foreach (TouchLocation t in input)
            {
                if (t.State == TouchLocationState.Pressed)
                {
                    foreach (Fish f in fishes)
                    {
                        if (f.Contains(t.Position))
                        {
                            f.dead = true;

                            if (f.colorOfFish == walrus.targetFish)
                            {
                                success.Play();
                                ScoreManager.playerScore += 10;
                            }
                            else
                            {
                                fail.Play();
                                ScoreManager.health--;
                            }
                        }
                    }
                }
            }
        }

        public void OnUpdate(GameTimerEventArgs e)
        {
            walrus.OnUpdate(e);

            for (int c = fishes.Count - 1; c >= 0; c--)
            {
                fishes[c].OnUpdate(e);
                if (fishes[c].dead)
                    fishes.RemoveAt(c);
            }

            spawnTime -= e.ElapsedTime;
            if (spawnTime <= TimeSpan.Zero)
            {
                spawnTime = totalTime;
                if (fishes.Count < maxFish)
                    fishes.Add(new Fish());
            }
        }


        public void OnDraw(SpriteBatch sb, GameTimerEventArgs e)
        {
            foreach (Fish f in fishes)
                f.OnDraw(sb, e);

            walrus.OnDraw(sb, e);
        }

    }
}
